<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelExport extends JModel{
	
	function getSurveyDetails(){
		$survey_id = JRequest::getVar("survey_id", "0");
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('id, published, title');
		$query->from('#__ijoomla_surveys_surveys');
		$query->where("id=".$survey_id);
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();
		return $result;
	}
	
	function getSurveyResponses(){
		$survey_id = JRequest::getVar("survey_id", "0");
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('count(*)');
		$query->from('#__ijoomla_surveys_session');
		$query->where("survey_id=".$survey_id." and completed=1 and last_page_id=0 and published=1");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadResult();
		return $result;	
	}
	
	function getSessions(){
		$survey_id = JRequest::getVar("survey_id", "0");
		$min_limit = JRequest::getVar("min_limit", "0");
		$max_limit = JRequest::getVar("max_limit", "0");
		$db =& JFactory::getDBO();		
		$sql = "select * from #__ijoomla_surveys_session where survey_id=".$survey_id." and completed=1 and last_page_id=0 and published=1 limit ".($min_limit-1).", ".$max_limit;
		$db->setQuery($sql);
		$db->query();
		$result = $db->loadAssocList();
		return $result;
	}
	
	function getQuestions(){
		$data = new JDate();
		$survey_id = JRequest::getVar("survey_id", "0");
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('*');
		$query->from('#__ijoomla_surveys_questions');
		$query->where("survey_id=".$survey_id." and published=1 and published_up <= '".$data."' and ((published_down >= '".$data."') or published_down='0000-00-00 00:00:00')");
		$query->order("ordering asc");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getUserValues($id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('name, username');
		$query->from('#__users');
		$query->where("id=".$id);
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function ExportData(){
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEssay.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultiplePerRow.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOneAnswer.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOneAnswerMenu.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultipleAnswer.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionOnePerRow.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEndedOneLine.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionEndedMoreLines.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionConstant.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionDateTime.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionDateTime.php");
		include_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."QuestionMultiplePerRowMenus.php");
		require_once(JPATH_ROOT.DS."components".DS."com_surveys".DS."helpers".DS."questions.php");
		
		$sessions = $this->getSessions();
		$questions = $this->getQuestions();
		$questions_ids = array();
		$header = "";
		$row = "";
		$i = JRequest::getVar("min_limit", "1");
		$header .= "RESPONSE,SUBMITTED,IP,USERNAME,NAME,";
		$class_object = new Question();
				
		foreach($questions as $key=>$value){			
			$class = "Question".$class_object->getClass($value["question_type"]);
			$object = new $class();
			$object_value = $object->getForHeader($value["id"], $value["title"]);
			$object_value = str_replace("\n\r", " ", $object_value);
			$object_value = str_replace("\r", " ", $object_value);
			$object_value = str_replace("\n", " ", $object_value);
			$header .= '"'.$object_value.'"'.",";
		}
		//eliminate last comma
		$header = substr($header, 0, -1);
				
		foreach($sessions as $key=>$value){
			$username = "Unregistered";
			$name = "Unregistered";
			if(trim($value["user_id"]) != "0"){
				$user = $this->getUserValues(trim($value["user_id"]));
				$username = $user["0"]["username"];
				$name = $user["0"]["name"];
			}
			$row .= $i.",".$value["played_time"].",".$value["ip"].",".$username.",".$name.",";
			foreach($questions as $key_q=>$value_q){
				$class = "Question".$class_object->getClass($value_q["question_type"]);
				$object = new $class();
				$object_value = $object->getForRow($value_q["id"], $value["id"]);
				$object_value = str_replace("\n\r", " ", $object_value);
				$object_value = str_replace("\r", " ", $object_value);
				$object_value = str_replace("\n", " ", $object_value);
				$row .= '"'.$object_value.'"'.",";
			}
			$row = substr($row, 0, -1);
			$row .= "\n";
			$i++;
		}
		
		$content = trim($header)."\n".$row;
		
		$survey_id = JRequest::getVar("survey_id", "0");
		$part = JRequest::getVar("part", "0");
		$file_name = "survey_".$survey_id."_part_".$part.".csv";
		
		header("Content-Type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=\"".$file_name."\"");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;			   
		exit();
	}
	
}

?>